package com.sky.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sky.constant.JwtClaimsConstant;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.entity.User;
import com.sky.exception.LoginFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.mapper.UserMapper;
import com.sky.properties.JwtProperties;
import com.sky.properties.WeChatProperties;
import com.sky.service.ShoppingCartService;
import com.sky.service.UserService;
import com.sky.utils.HttpClientUtil;
import com.sky.utils.JwtUtil;
import com.sky.vo.SetmealVO;
import com.sky.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
private ShoppingCartMapper  shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
   @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //1根据传参进行查询
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        //2判断当前菜品或者套餐是否在购物车中
       if (shoppingCartList.size() > 0){
           log.info("购物车中有商品，更新数量");
           shoppingCart.setNumber(shoppingCart.getNumber() + 1);
           shoppingCartMapper.update(shoppingCart);
       }else {
           log.info("购物车中没有商品，新增数量");
           shoppingCart.setNumber(1);
           //判断添加到购物车的是菜品还是套餐
           Long dishId = shoppingCartDTO.getDishId();
           if (dishId != null){
               Dish dish = dishMapper.queryById(dishId);
               log.info("添加到购物车的是菜品");
               shoppingCart.setName(dish.getName());
               shoppingCart.setImage(dish.getImage());
               shoppingCart.setAmount(dish.getPrice());
           }else if (ObjectUtils.isNotEmpty(shoppingCartDTO.getSetmealId())){
               log.info("添加到购物车的是套餐");
               SetmealVO setmealVO =setmealMapper.queryById(shoppingCartDTO.getSetmealId());
               shoppingCart.setName(setmealVO.getName());
               shoppingCart.setImage(setmealVO.getImage());
               shoppingCart.setAmount(setmealVO.getPrice());
           }
           shoppingCartMapper.insert(shoppingCart);

       }


    }

    /**
     * 查询购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        final ShoppingCart cart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        List<ShoppingCart> cartList = this.shoppingCartMapper.list(cart);
        return cartList;

    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    /**
     * 修改购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //1获取用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断购物车里是否有商品
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
//        for (ShoppingCart cart : shoppingCartList) {
//            String dishFlavor = cart.getDishFlavor();
//            if (dishFlavor.equals(shoppingCartDTO.getDishFlavor()) ){
//
//            }
//        }
        ShoppingCart cart = null;
        for (int i = 0; i < shoppingCartList.size(); i++) {
            if (shoppingCartList.get(i).getDishFlavor().equals(shoppingCartDTO.getDishFlavor())) {
                cart = shoppingCartList.get(i);
            }
        }
        if (cart.getNumber() > 1) {
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.update(cart);
        }else {
            shoppingCartMapper.delete(cart);

        }





        //3判断购物车中的商品是否为套餐

 
    }
}
